package com.liuheng.spzx.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuheng.spzx.model.dto.h5.OrderInfoDto;
import com.liuheng.spzx.model.entity.order.OrderInfo;
import com.liuheng.spzx.model.entity.order.OrderItem;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import com.liuheng.spzx.model.vo.h5.TradeVo;
import com.liuheng.spzx.order.mapper.OrderItemMapper;
import com.liuheng.spzx.order.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value="/api/order/orderInfo")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private OrderItemMapper orderItemMapper;
    //购物车结算
    @GetMapping("/auth/trade")
    public Result<TradeVo> trade(){
        TradeVo  trade=orderInfoService.getTrade();
        return Result.build(trade, ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/auth/submitOrder")
    public Result<Long> submitOrder(@RequestBody OrderInfoDto orderInfoDto){
        Long orderId=orderInfoService.submitOrder(orderInfoDto);
        return Result.build(orderId,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable Long orderId){
        OrderInfo byId = orderInfoService.getById(orderId);
        return Result.build(byId,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/buy/{skuId}")
    public Result<TradeVo> buy(@PathVariable Long skuId){
        TradeVo tradeVo=orderInfoService.buy(skuId);
        return Result.build(tradeVo,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/auth/{page}/{limit}")
    public Result<Page<OrderInfo>> list(@PathVariable int page, @PathVariable int limit, @RequestParam(required = false,defaultValue = "") Integer orderStatus){
        Page<OrderInfo> page1 = new Page<>(page, limit);
        Page<OrderInfo> findpage=orderInfoService.findpage(page1,orderStatus);
        return Result.build(findpage,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("auth/getOrderInfoByOrderNo/{orderNo}")
    public OrderInfo getOrderInfoByOrderNo(@PathVariable String orderNo){
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(orderNo!=null,OrderInfo::getOrderNo,orderNo);
        OrderInfo orderInfo = orderInfoService.getOne(wrapper);
        LambdaQueryWrapper<OrderItem> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(OrderItem::getOrderId,orderInfo.getId());
        orderInfo.setOrderItemList(orderItemMapper.selectList(wrapper1));
        return orderInfo;
    }
    @GetMapping("auth/updateOrderStatusPayed/{orderNo}/{orderStatus}")
    public Result updateOrderStatus(@PathVariable(value = "orderNo") String orderNo , @PathVariable(value = "orderStatus") Integer orderStatus) {
        orderInfoService.updateOrderStatus(orderNo , orderStatus);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }
}
